﻿/*
 * Twipler - Twitter Web Client with features.
 *  
 *  Copyright (C) 2009, 2010. Ian Quigley
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details. 
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */

using System;
using Twipler.App.Render;
using Twipler.App.DataDoc;
using Twipler.Accounts;
using Twipler.Data.Entity.Theme;
using System.Collections.Generic;
using Twipler.Data.Access;
using NHibernate.Criterion;

namespace Twipler.Data.Entity.Twipler 
{
    public enum VisitorRole
    {
        Unknown        = 0,
        Registered     = 0x00000001,
        StyleCreator   = 0x00000008,
        StyleModerator = 0x00000010,
        ThemeCreator   = 0x00000080,
        ThemeModerator = 0x00001000,
        Admin          = 0x20000000,
        SuperAdmin     = 0x40000000
    }

    public class VisitorAnon : Visitor
    {
        public override bool IsAnonymous { get { return true; } }
        public override int Id { get { return -1; } set {}}
        public override string ScreenName { get { return "Anonymous"; } set {} }
        public override Design Design {get { return null; } set {} }
        public override IDataDoc Details { get { return null; } set { } }
        public override bool HasRole(VisitorRole role) { return false; }
        public override DateTime LastLogin { get { return DateTime.MinValue; } set { } }
        public override int LoginCount  { get { return 0; } set { }}
        public override string  Name { get { return "Anonymous"; } set {} }
        public override VisitorRole Role { get { return VisitorRole.Unknown;} set {}}
        public override Style Style { get { return null; } set { } }
        public override TwitterUser TwitterUser { get { return null ; }}
        public override bool UserCanCreate() { return false; }
        public override bool UserCanModify() { return false;  }
        public override string OAuthToken { get { return null; } set { } }
    	public override List<Visitor> Alias { get { return null; } set { } }
	}

    public class Visitor : Base
    {
        private Theme.Design _design;

        public static Visitor Anonymous = new VisitorAnon();
        public virtual bool IsAnonymous { get { return false; } }

        [EditReadonly] 
        [EditOrder(1)]
        public virtual string ScreenName { get; set; }

        [EditReadonly]
        [EditOrder(5)]
        public virtual string Name { get; set; }
        
        [EditOrder(10)]
        public virtual Theme.Design Design
        {
            get { return _design; } 
            set
            {
                _design = value;
                Style = _design != null ? _design.DefaultStyle : null;
            }
        }

        [EditOrder(12)]
        public virtual Theme.Style Style{ get; set; }

        [EditOrder(20)]
        [EditCondition(EditCondition.Condition.IsSuperAdmin)]
        public virtual VisitorRole Role { get; set; }

        [EditOrder(40)]
        [EditReadonly]
        public virtual DateTime LastLogin { get; set; }

        [EditOrder(50)]
        [EditReadonly]
        public virtual int LoginCount { get; set; }

        [EditIgnore]
        public virtual IDataDoc Details { get; set; }

        private TwitterUser _twitterUser;

        [EditIgnore]
        public virtual TwitterUser TwitterUser
        {
            get
            {
                if (_twitterUser == null)
                    _twitterUser = new TwitterUser(Id);

                return _twitterUser;
            }
        }

        [EditIgnore]
        public virtual bool IsPrimaryIdentity { get;  set; }
        
        private List<Visitor> _aliasVisitors = null;

		[EditIgnore]
        public virtual List<Visitor> Alias
        {
			get
			{
				if (_aliasVisitors == null)
				{
					List<Alias> aliasLinks = Factory.Alias.GetByCriteria( Restrictions.Eq( "Id", Id ) );

					
                    int primaryId = 0;
                    _aliasVisitors = new List<Visitor>();

                    foreach (Alias alias in aliasLinks)
                    {
                        _aliasVisitors.Add(Factory.Visitor.GetById(alias.OtherTwitterId, false));
                        if (alias.Id == alias.OtherTwitterId)
                            primaryId = alias.Id;
                    }

					_aliasVisitors.ForEach( v => v.IsPrimaryIdentity = v.Id == primaryId);

					IsPrimaryIdentity = true;
				}

				return _aliasVisitors;
			}
			set
			{
				_aliasVisitors = value;
			}
        }

		
		[EditIgnore]
		public virtual string OAuthToken { get; set; }


        public Visitor() : base()
        {
            LastLogin = DateTime.Now;
        }

        public override string ToString()
        {
            return ScreenName;
        }

        public virtual bool HasRole(VisitorRole role)
        {
            return ((int)role <= (int)Role);
        }

        public override bool UserCanModify()
        {
            return (App.Current.Visitor.IsAnonymous) ||
                   (App.Current.Visitor.HasRole(VisitorRole.SuperAdmin) ||
                   (App.Current.Visitor.Id == this.Id));
        }

        public override bool UserCanCreate()
        {
			// Only anonymous users can create new users. (Login)
            return (App.Current.Visitor.IsAnonymous);
        }
    }
}
